using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Belmost4PDA.Logic
{
    public class StateDispatcher
    {
        public static int HorizMargins = 10;
        public static int VertMargins = 5;
        public static int ButtonHeight = 20; 
        public static StateDispatcher __instance = null;
        public StatesBuilder builder;
        /// <summary>
        /// Initializes a new instance of the <see cref="StateDispatcher"/> class.
        /// </summary>
        public StateDispatcher()
        {
            builder = new StatesBuilder();
        }
        /// <summary>
        /// Gets the dispatcher.
        /// </summary>
        /// <returns></returns>
        public static StateDispatcher GetDispatcher()
        {
            if(__instance == null)
                __instance = new StateDispatcher();
            return __instance;
        }

        /// <summary>
        /// Quits from state.
        /// </summary>
        public virtual void QuitFromState()
        {
            ShowMainMenu();
        }

        /// <summary>
        /// Clears the form.
        /// </summary>
        public virtual void ClearForm()
        {
            if (WorkForm != null)
            {
                WorkForm.Controls.Clear();
                WorkForm.Menu = new MainMenu();
            }
        }

        private List<StateRecord> states = null;
        /// <summary>
        /// Shows the main menu.
        /// </summary>
        public virtual void ShowMainMenu()
        {
            // get params
            ClearForm();
            if (states == null)
            {
                states = builder.Create();
            }
            int buttonNum = 0;
            foreach (StateRecord stateRecord in states)
            {
                Button button = new Button();
                button.Text = stateRecord.TextToShow;
                button.Left = HorizMargins;
                if (WorkForm != null) button.Width = WorkForm.Width - 2*HorizMargins;
                button.Height = ButtonHeight;
                button.Top = VertMargins + (ButtonHeight + VertMargins) * buttonNum;
                button.Click += buttonMainMenu_Click;
                button.Tag = stateRecord;
                buttonNum++;
                button.Enabled = stateRecord.Enabled;
                if (WorkForm != null) WorkForm.Controls.Add(button);
            }

        }

        /// <summary>
        /// Handles the Click event of the buttonMainMenu control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        public static void buttonMainMenu_Click(object sender, EventArgs e)
        {
            GetDispatcher().ClearForm();
            if (sender != null)
            {
                if(((StateRecord)((Button)sender).Tag).StartPanel != 0)
                    ((StateRecord)((Button)sender).Tag).RunState(GetDispatcher().WorkForm);
                else
                    if (GetDispatcher().WorkForm != null)
                    {
                        // close database
                        DatabaseWorkPDA.DataBase.Disconnect();
                        GetDispatcher().WorkForm.Close();
                    }
            }
        }

        private Form WorkForm = null;
        /// <summary>
        /// Shows the main menu.
        /// </summary>
        /// <param name="MainForm">The main form.</param>
        public virtual void ShowMainMenu(Form MainForm)
        {
            WorkForm = MainForm;
            ShowMainMenu();
        }

    }
}
